package com.yunshang.shoppingmall.shopping.User;

import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.entity.Finace.Finace;
import com.yunshang.shoppingmall.service.Finace.FinaceService;
import com.yunshang.shoppingmall.service.OrderService;
import com.yunshang.shoppingmall.service.SysUserNoService;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*分享赚*/
@RequestMapping("/wap")
@Controller
public class ShareEaringController {

    @Autowired
    SysUserNoService sysUserNoService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserRankService userRankService;
    @Autowired
    FinaceService finaceService;

    //推荐人收益
    @ResponseBody
    @RequestMapping("/sharerEaring")
    public Map<String, Object> sharerEaring(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        Timestamp startTime = null;
        Timestamp endTime = null;
        //获取用户的信息
        String userNoId = request.getParameter("userNoId");
        double dayProfitSum = 0.00;
        double weekProfitSum = 0.00;
        double monthProfitSum = 0.00;
        int userNoIds = 0;
        if (userNoId != null && userNoId != "") {
            userNoIds = Integer.parseInt(userNoId);
        }


        //查询当天用户所有订单
        startTime = new Timestamp(DateUtils.getDayBegin().getTime());
        endTime = new Timestamp(DateUtils.getDayEnd().getTime());
        OrderToData orderToData = new OrderToData();
        orderToData.setStartTime(startTime);
        orderToData.setEndTime(endTime);
        orderToData.setUserNoId(userNoIds);
        orderToData.setFinaceType("888");
        orderToData.setClassify(1);
        List<Finace> dayFinaceList = finaceService.selectFinaceByUserIdAndFinaceType(orderToData);
        for (int i = 0; i < dayFinaceList.size(); i++) {
            dayProfitSum += dayFinaceList.get(i).getAmount().doubleValue();
        }
        //给购买用户的推荐人分享赚
        map.put("dayShareEaringMoney", dayProfitSum);


        //查询每周
        Date date = new Date();
        String date1 = DateUtils.getTimeInterval(date);
        String dateTime[] = date1.split(",");
        startTime = Timestamp.valueOf(dateTime[0]);
        endTime = Timestamp.valueOf(dateTime[1]);
        orderToData.setStartTime(startTime);
        orderToData.setEndTime(endTime);
        List<Finace> weekFinaceList = finaceService.selectFinaceByUserIdAndFinaceType(orderToData);
        for (int i = 0; i < weekFinaceList.size(); i++) {
            weekProfitSum += weekFinaceList.get(i).getAmount().doubleValue();
        }
        //给购买用户的推荐人分享赚
        map.put("weekShareEaringMoney", weekProfitSum);

        //每月
        Date startMonth = DateUtils.getBeginDayOfMonth();
        Date endMonth = DateUtils.getEndDayOfMonth();
        startTime = new Timestamp(startMonth.getTime());
        endTime = new Timestamp(endMonth.getTime());
        orderToData.setStartTime(startTime);
        orderToData.setEndTime(endTime);
        List<Finace> monthFinaceList = finaceService.selectFinaceByUserIdAndFinaceType(orderToData);
        for (int i = 0; i < monthFinaceList.size(); i++) {
            monthProfitSum += monthFinaceList.get(i).getAmount().doubleValue();
        }
        //给购买用户的推荐人分享赚
        map.put("monthShareEaringMoney", monthProfitSum);
        map.put("msg","1");
        return map;
    }




}